package com.qf.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsTopicMapper;
import com.qf.pojo.DtsTopic;
import com.qf.service.AdminTopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;


@Service
public class AdminTopicServiceImpl implements AdminTopicService {

    @Autowired
    private DtsTopicMapper dtsTopicMapper;

    @Override
    public IPage<DtsTopic> selectTopicList(Integer page, Integer limit, String title, String subtitle, String sort, String order) {

        IPage<DtsTopic> iPage = new Page<DtsTopic>(page, limit);

        QueryWrapper<DtsTopic> wrapper = new QueryWrapper<>();
        wrapper.eq( "deleted", false);
        wrapper.orderByAsc("asc".equalsIgnoreCase(order),sort);
        wrapper.orderByDesc("desc".equalsIgnoreCase(order),sort);
        wrapper.like(!StringUtils.isEmpty(title), "title", title);
        wrapper.like(!StringUtils.isEmpty(subtitle), "subtitle",subtitle );

        IPage<DtsTopic> result = dtsTopicMapper.selectPage(iPage, wrapper);
        return result;
    }

    @Override
    public DtsTopic insertTopic(DtsTopic dtsTopic) {

        dtsTopic.setAddTime(new Date());
        dtsTopic.setUpdateTime(new Date());
        dtsTopic.setDeleted(false);
        dtsTopicMapper.insert(dtsTopic);

        QueryWrapper<DtsTopic> wrapper = new QueryWrapper<>();
        wrapper.select("id");
        wrapper.eq("title" ,dtsTopic.getTitle());
        wrapper.eq( "deleted", false);

        DtsTopic result= dtsTopicMapper.selectOne(wrapper);

        return result;
    }

    @Override
    public DtsTopic updateTopic(DtsTopic dtsTopic) {
        dtsTopic.setUpdateTime(new Date());
        dtsTopicMapper.updateById(dtsTopic);
        return dtsTopic;
    }

    @Override
    public void deleteTopic(Integer id) {
        DtsTopic dtsTopic = new DtsTopic();
        dtsTopic.setId(id);
        dtsTopic.setDeleted(true);
        dtsTopicMapper.updateById(dtsTopic);
    }
}
